package com.glq.book.dao;

import com.glq.book.entity.Borrow;
import com.glq.book.entity.Order;
import com.glq.book.util.JdbcUtil;
import com.glq.book.vo.OrderVO;
import com.sun.corba.se.impl.resolver.SplitLocalResolverImpl;
import com.sun.org.apache.xpath.internal.operations.Or;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {

    private JdbcUtil jdbcUtil = new JdbcUtil();
    private Connection connection = null;
    private ResultSet resultSet = null;
    private PreparedStatement preparedStatement = null;
    private String sql = "";
//    private String orderId = "";

    /**
     * 买书,添加订单
     *
     * @param order
     * @return
     */
    public boolean insertOrder(OrderVO order,String orderId) {
        try {
            sql = "insert into `order`(book_name,username,`number`,money,buy_time,order_id) " +
                    "values(?,?,?,?,?,?)";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, order.getBookName());
            preparedStatement.setString(2, order.getUsername());
            preparedStatement.setInt(3, order.getNumber());
            preparedStatement.setInt(4, order.getMoney());
            preparedStatement.setDate(5, new Date(System.currentTimeMillis()));
            preparedStatement.setString(6, orderId);
            if (!preparedStatement.execute()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return false;
    }

    /**
     * 买书记录
     * @param username
     * @return
     */
    public List<Order> selectOrderListByUser(String username) {
        List<Order> list = new ArrayList<>();
        try {
            sql = "select * from `order` where username = ? order by buy_time DESC ";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(new Order(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getInt(4),
                        resultSet.getInt(5),
                        resultSet.getDate(6),
                        resultSet.getString(7)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return list;
    }

    public List<Order> selectOrderList() {
        List<Order> list = new ArrayList<>();
        try {
            sql = "select * from `order` order by buy_time DESC";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(new Order(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getInt(4),
                        resultSet.getInt(5),
                        resultSet.getDate(6),
                        resultSet.getString(7)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return list;
    }

    public Order selectByOrderId(String orderId) {
        Order order = new Order();
        try {
            sql = "select * from `order` where order_id = ? order by buy_time DESC";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, orderId);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                order = new Order(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getInt(4),
                        resultSet.getInt(5),
                        resultSet.getDate(6),
                        resultSet.getString(7));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return order;
    }

}
